//
//  PlayViewController.m
//  播放器HW
//
//  Created by dllo on 15/12/9.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "PlayViewController.h"
#import "STKAudioPlayer.h"

@interface PlayViewController ()



@end

@implementation PlayViewController

- (void)dealloc
{
    [_coverV release];
    [_song release];
    [_singer release];
    [_lyrics release];
    [_playB release];
    [_stopB release];
    [_volS release];
    [_timeS release];
    [_timer release];
    [super dealloc];
}

+ (instancetype)sharePlayViewController
{
    static PlayViewController *playVC = nil;
    //    if (nil == rootVC) {
    //        rootVC = [[RootViewController alloc] init];
    //    }
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        
        playVC = [[PlayViewController alloc] init];
        
    });
    return playVC;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    
    
    STKAudioPlayerOptions playerOptions = {YES, YES, {50, 100, 200, 400, 800, 1600, 2600, 16000}};
    
    self.player = [[[STKAudioPlayer alloc] initWithOptions:playerOptions] autorelease];
    self.player.volume = 1;
    
    self.coverV = [[UIImageView alloc] initWithFrame:CGRectMake(70, 60, 250, 250)];
    self.coverV.backgroundColor = [UIColor blueColor];
    self.coverV.layer.cornerRadius = 125;
    [self.view addSubview:self.coverV];
    [_coverV release];
    
    self.playB = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playB.frame = CGRectMake(100, 600, 50, 30);
    [self.playB setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.playB setImage:[UIImage imageNamed:@"list_icon_music_mv@2x.png"] forState:UIControlStateNormal];
    [self.playB setImage:[UIImage imageNamed:@"download_icon_allstop@2x.png"] forState:UIControlStateSelected];
    [self.playB addTarget:self action:@selector(playAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.playB];
    
    self.stopB = [UIButton buttonWithType:UIButtonTypeCustom];
    self.stopB.frame = CGRectMake(250, 600, 50, 30);
    [self.stopB setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.stopB setImage:[UIImage imageNamed:@"list_icon_music_cd@2x.png"] forState:UIControlStateNormal];
    [self.stopB addTarget:self action:@selector(stopAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.stopB];
    
    self.volS = [[UISlider alloc] initWithFrame:CGRectMake(40, 380, 300, 30)];
    [self.volS addTarget:self action:@selector(volAction) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.volS];
    [_volS release];
    
    self.timeS = [[UISlider alloc] initWithFrame:CGRectMake(40, 460, 300, 30)];
    self.timeS.maximumValue = 193;
    [self.timeS addTarget:self action:@selector(timeAction) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.timeS];
    [_timeS release];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 30, 20, 20);
//    button.backgroundColor = [UIColor blackColor];
    [button setImage:[UIImage imageNamed:@"list_btn_forword@2x.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (void)buttonAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

- (void)trackAction
{
    NSLog(@"当前分钟:%ld, 当前秒:%ld", (NSInteger)self.player.progress / 60, (NSInteger)self.player.progress % 60);
    NSLog(@"总分钟:%ld, 总秒:%ld", (NSInteger)self.player.duration / 60, (NSInteger)self.player.duration % 60);
    
    // 进度条更新
    self.timeS.value = self.player.progress;
}

- (void)timeAction
{
    // 跳转时间
    [self.player seekToTime:self.timeS.value];
}

- (void)volAction
{
    // 设置音量
    self.player.volume = self.volS.value;
    NSLog(@"%f", self.volS.value);
}

- (void)playAction
{
    
    if (self.player.state == STKAudioPlayerStatePlaying) {
        // 暂停
        [self.player pause];
        self.playB.selected = NO;
        
    } else if (self.player.state == STKAudioPlayerStatePaused) {
        // 继续
        [self.player resume];
        self.playB.selected = YES;
        
    } else {
        
        // 播放
        [self.player play:@"http://dl.118100.cn:9495/res/V/1388/mp3/33/67/81/1388336781040800.mp3?sid=00000000000&filesize=769461&serviceid=100&nettype=0&resid=63848673&Mode=online&softid=240655972"];
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(trackAction) userInfo:nil repeats:YES];
        
        self.playB.selected = YES;
    }
}

- (void)stopAction
{
    // 停止
    [self.player stop];
    self.playB.selected = NO;
    // 停止定时器
    [self.timer invalidate];
    //    self.timer = nil;
    
    self.timeS.value = 0;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
